"""
File: yaml_item.py
Author: ershixiong
Date: 2024/3/22
describe:

"""
import pytest

from ddt_framework_02.testcase import TestCase
from ddt_framework_02.yaml_exception import YamlException


class YamlItem(pytest.Item):
    def __init__(self, *, spec, **kwargs):
        super().__init__(**kwargs)
        self.spec = spec
        self.testcase: TestCase = TestCase(**spec)

    def runtest(self):
        self.testcase.run()

    def repr_failure(self, excinfo):
        """Called when self.runtest() raises an exception."""
        if isinstance(excinfo.value, YamlException):
            return "\n".join(
                [
                    "usecase execution failed",
                    "   spec failed: {1!r}: {2!r}".format(*excinfo.value.args),
                    "   no further details known at this point.",
                ]
            )
        return super().repr_failure(excinfo)

    def reportinfo(self):
        return self.path, 0, f"usecase: {self.name}"
